﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XCode;
using XCode.Configuration;
using menu = NewLife.CommonEntity.Menu;
using ExtAspNet;
using NewLife.CommonEntity;
namespace V8.SystemSet
{
    public partial class Menu : V8Core.Entities.BLL.PageBase
    {
        public FieldItem FIdentity = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("~/SystemSet/MenuForm.aspx", "新增菜单");
                BindGrid();
            }
        }
        private void BindGrid()
        {
            Grid1.Title = menu.Meta.Table.Description;
            
            foreach (FieldItem item in menu.Meta.Fields)
            {
                if (item.IsIdentity) FIdentity = item;
                ExtAspNet.BoundField boundField1 = new ExtAspNet.BoundField();
                boundField1.DataTooltipField = item.Name;
                boundField1.DataField = item.Name;
                boundField1.DataFormatString = "{0}";
                boundField1.HeaderText = item.Description;
                Grid1.Columns.Add(boundField1);
            }
            #region 编辑操作
            if (Acquire(PermissionFlags.Update))
            {
                if (FIdentity != null)
                {
                    ExtAspNet.WindowField WindowField1 = new ExtAspNet.WindowField();
                    //                    <ext:WindowField Text="编辑" WindowID="Window1" Title="编辑" DataIFrameUrlFields="Id"
                    //            DataIFrameUrlFormatString="~/admin/menu_edit.aspx?id={0}" Width="50px" />
                    WindowField1.Text = "编辑";
                    WindowField1.Title = "编辑";
                    WindowField1.HeaderText = "编辑";
                    WindowField1.WindowID = "Window1";
                    WindowField1.DataIFrameUrlFormatString = "~/SystemSet/MenuForm.aspx?id={0}";
                    WindowField1.Width = 50;
                    WindowField1.DataIFrameUrlFields = FIdentity.Name;
                    Grid1.Columns.Add(WindowField1);
                }
            }
            #endregion
            #region 删除操作
            if (Acquire(PermissionFlags.Delete))
            {
                LinkButtonField DelField = new LinkButtonField();
                DelField.Text = "删除";
                DelField.ConfirmIcon = MessageBoxIcon.Question;
                DelField.ConfirmText = "确定删除此" + menu.Meta.Table.Description + "?";
                DelField.ConfirmTarget =  Target.Top;
                DelField.HeaderText = "删除";
                DelField.CommandName = "Delete";
                DelField.Width = 50;
                Grid1.Columns.Add(DelField);
            }
            #endregion
            if (FIdentity != null) Grid1.DataKeyNames =new String[]{ FIdentity.Name};
            Grid1.DataSource = menu.Root.AllChilds;
            Grid1.DataBind();
        }
        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            int menuId = Convert.ToInt32(Grid1.DataKeys[e.RowIndex][0]);

            if (e.CommandName == "Delete")
            {
                menu.Delete(menu._.ID + "=" + menuId.ToString());
                //menu.Delete(menu._.ID, menuId);

                BindGrid();
            }
            //else if (e.CommandName == "Show")
            //{
            //    bool rowChecked = Convert.ToBoole?an(Grid1.Rows[e.RowIndex].States[e.ColumnIndex]);

            //    XMenu menu = XMenu.FetchByID(menuId);
            //    menu.IsMainMenu = rowChecked;
            //    menu.Save(User.Identity.Name);

            //    BindGrid();
            //}
        }
        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();

        }
    }
}